package com.mobile.check.util;

import android.util.Log;

import com.rt.BASE64Decoder;
import com.rt.BASE64Encoder;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;

/**
 * Base64编码解码
 * @author changxiaofei
 * @time 2013-3-26 下午4:07:03
 */
public class CryptUtils {
	private BASE64Encoder base64Encoder = new BASE64Encoder();
	private BASE64Decoder base64Decoder = new BASE64Decoder();
	// 设置密钥,最少为24位
	private static String DEFAULT_KEY = "com.ultrapower.eoms4.deckey";
	private static String DEFAULT_IV = "cryptpwd";
	private Cipher ENCRYPT_MODE_CIPHER;// 加密
	private Cipher DECRYPT_MODE_CIPHER;// 解密

	private CryptUtils() {
	}

	private CryptUtils(String key) {
		SecureRandom sr = new SecureRandom();
		// 从原始密匙数据创建一个DESKeySpec对象
		DESedeKeySpec dks;
		try {
			dks = new DESedeKeySpec(key.getBytes());
			// 创建一个密匙工厂，然后用它把DESKeySpec转换成一个SecretKey对象
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
			SecretKey skey = keyFactory.generateSecret(dks);
			// Cipher对象实际完成加密操作
			ENCRYPT_MODE_CIPHER = Cipher.getInstance("DESede/CBC/PKCS5Padding");
			DECRYPT_MODE_CIPHER = Cipher.getInstance("DESede/CBC/PKCS5Padding");
			// 用密匙初始化Cipher对象
			IvParameterSpec iv = new IvParameterSpec(DEFAULT_IV.getBytes());
			ENCRYPT_MODE_CIPHER.init(Cipher.ENCRYPT_MODE, skey, iv, sr);
			DECRYPT_MODE_CIPHER.init(Cipher.DECRYPT_MODE, skey, iv, sr);
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (InvalidKeySpecException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (InvalidAlgorithmParameterException e) {
			e.printStackTrace();
		}
	}

	public static CryptUtils getInstance() {
		String s = DEFAULT_KEY;

		CryptUtils instance = new CryptUtils(s);
		return instance;
	}

	public static CryptUtils getInstance(String... key) {
		String s = null;
		if (key == null || key.length == 0 || key[0] == null || key[0].length() < 24) {
			s = DEFAULT_KEY;
		} else {
			s = key[0];
		}
		CryptUtils instance = new CryptUtils(s);
		return instance;
	}

	/**
	 * 加密明文
	 * @param data 明文
	 * @return 密文
	 * @throws Exception
	 */
	public  String encode(String data) {
		if (data == null) {
			data = "";
		}
		byte[] pasByte = null;
		try {
			pasByte = ENCRYPT_MODE_CIPHER.doFinal(data.getBytes("utf-8"));
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return base64Encoder.encode(pasByte);
	}

	/**
	 * 解密密文
	 * @param data 密文
	 * @return 明文
	 * @throws Exception
	 */
	public  String decode2(String data) {
		if (data == null || "".equals(data.trim())) {
			return "";
		}
		if(data.startsWith("<") && data.endsWith(">")){
			return data;
		}
		if(data.equalsIgnoreCase("OK")){
			return data;
		}
		byte[] pasByte = null;
		String txt = null;
		try {
			pasByte = DECRYPT_MODE_CIPHER.doFinal(base64Decoder.decodeBuffer(data));
			txt = new String(pasByte, "UTF-8");
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}catch (Exception ex){
			ex.printStackTrace();
		}

		Log.i("CryptUtils","responseStr = "+txt);
		return txt;
	}

}
